import dayjs from "dayjs";
/**
 * 防抖函数
 * @param fn
 * @param wait time
 * @param immediate 是否立即执行
 * @returns {function(): void}
 */
import {message} from "ant-design-vue";

export function immediatelyDebounce(fn, wait = 1000, immediate = true) {
    let timer;
    return function () {
        if (timer) clearTimeout(timer);
        if (immediate) {
            // 如果已经执行过，不再执行
            let callNow = !timer;
            timer = setTimeout(() => {
                timer = null;
            }, wait)
            if (callNow) {
                fn.apply(this, arguments)
            }
        } else {
            timer = setTimeout(() => {
                fn.apply(this, arguments)
            }, wait);
        }
    }
}

/**
 * 节流函数
 * @param fn
 * @param wait time
 * @returns {function(): void}
 */
export function throttle(fn, wait = 200) {
    let timer;
    return function () {
        if (!timer) {
            timer = setTimeout(() => {
                timer = null
                fn.apply(this, arguments)
            }, wait)
        }
    }
}

export function dateFormat(date, fmt = 'YYYY-mm-dd') {
    let ret;
    const opt = {
        "Y+": date.getFullYear().toString(),        // 年
        "m+": (date.getMonth() + 1).toString(),     // 月
        "d+": date.getDate().toString(),            // 日
        "H+": date.getHours().toString(),           // 时
        "M+": date.getMinutes().toString(),         // 分
        "S+": date.getSeconds().toString()          // 秒
    };
    for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        }
    }
    return fmt;
}


export function copyText(text) {
    let input = document.createElement("input");
    input.value = text
    document.body.appendChild(input);
    input.select()
    document.execCommand('copy')
    message.success('复制成功')
    document.body.removeChild(input);
}

/**
 *
 * @param name 前端name路径
 * @param menuId 菜单id
 */
export function getAuth(name) {
    const menuList = JSON.parse(sessionStorage.getItem('menuList'));
    const father = menuList.find(item => item.webUrl === name)
    const child = menuList.filter(item => item.possess === 1).filter(item => item.parentMenuId === father.menuId)
    console.log(child)
    return child.map(item => item.identification)
}

/**
 * 获取时间区间
 * num 提前的天数
 * @returns {Array}
 */
export function defaultTimeDate(num = 7, fmt = 'YYYY-MM-DD') {
    const daysNum = dayjs().date()
    const beforeDate = dayjs().date(daysNum - (num - 1))
    return [dayjs(beforeDate.$d).format(fmt), dayjs(new Date()).format(fmt)]
}

/**
 *  按钮显示隐藏
 * @param name
 * @param value
 * @return {boolean}
 */
export function hasPermission(name, value) {
    const permissionList = getAuth(name)
    console.log(permissionList)
    return permissionList.indexOf(value) !== -1
}

/**
 *  日期区间查询处理 包头包尾
 * @param timeStampArr {Array}
 * @return {*}
 */
export function dateFormatTimeStamp(timeStampArr) {
    const [beforeDate, afterData] = timeStampArr || []
    if (beforeDate && afterData) {
        const isStr = Object.prototype.toString.call(beforeDate) == "[object String]"
        return [dateFormat(isStr ? new Date(beforeDate) : beforeDate._d), dateFormat(new Date(new Date(afterData).getTime() + (24 * 60 * 60 * 1000)))]
    } else {
        return []
    }
}
